<!-- 主链管理 -->
<template>
  <div class="table-box">
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item>
          <el-select v-model="form.chain_name" placeholder="主链名称">
            <el-option
              v-for="item in chainsList"
              :key="item.chain_id"
              :label="item.chain_name"
              :value="item.chain_name"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
          <el-button type="primary" plain @click="onAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="id" align="center" />
          <el-table-column prop="chain_name" label="主链名称" align="center" />
          <el-table-column
            prop="is_deposit"
            label="是否允许充值"
            align="center"
            :formatter="formatter"
          />
          <el-table-column
            prop="is_withdraw"
            label="是否允许提现"
            align="center"
            :formatter="formatter"
          />
          <el-table-column
            prop="createtime"
            label="添加时间"
            align="center"
            :formatter="formatterTime"
            width="180"
          />
          <el-table-column
            prop="updatetime"
            label="更新时间"
            align="center"
            :formatter="formatterTime"
            width="180"
          />
          <el-table-column
            label="操作"
            align="center"
            fixed="right"
            width="120"
          >
            <template #default="scope">
              <el-button
                type="primary"
                plain
                @click="onEdit(scope.$index, scope.row)"
                >编辑</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>

    <DialogForm
      @cancel="cancel"
      @closeDialog="closeDialog"
      @submitForm="submit"
      :dialogFormVisible="dialogFormVisible"
      :dialogForm="dialogForm"
      :btnLoading="btnLoading"
    />
  </div>
</template>

<script setup>
import { ref, onMounted, reactive, computed } from "vue";

import { formatterTime } from "@/utils/utils";
import { mainnetListApi, mainnetEditApi } from "@/api/ExAssistant/wallet";
import { GlobalStore } from "@/store";
import DialogForm from "./components/dialogForm.vue";
const globalStore = GlobalStore();

const chainsList = computed(() => globalStore.chains);

const dialogFormVisible = ref(false);
const tableData = ref([]);

const loading = ref(false);
const btnLoading = ref(false);
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});
const formData = {
  chain_name: "",
};
const form = reactive({ ...formData });
const dialogFormData = {
  chain_name: "",
  chain_id: "",
  is_deposit: false,
  is_withdraw: false,
};

const dialogForm = reactive({ ...dialogFormData });

const formatter = (row, column, cellValue, index) => {
  return cellValue == 1 ? "允许" : "不允许";
};

// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    chain_name: form.chain_name,
  };
};

//重置
const onReset = () => {
  Object.assign(form, formData);
};
// 查询
const onQuery = () => {
  getTbaleData();
};
//新增
const onAdd = () => {
  dialogFormVisible.value = true;
};
//关闭弹窗
const closeDialog = () => {
  dialogFormVisible.value = false;
  Object.assign(dialogForm, dialogFormData);
};

// 取消
const cancel = () => {
  dialogFormVisible.value = false;
  Object.assign(dialogForm, dialogFormData);
};
// 提交
const submit = async (param) => {
  console.log(param);
  try {
    btnLoading.value = true;
    const res = await mainnetEditApi(param);
    ElMessage({
      message: res.message,
      type: "success",
    });
    dialogFormVisible.value = false;
    btnLoading.value = false;
    getTbaleData();
  } catch (error) {
    btnLoading.value = false;
    dialogFormVisible.value = false;
  }
};

// 表格按钮操作
const onEdit = (index, row) => {
  console.log(row);
  Object.assign(dialogForm, {
    chain_name: row.chain_name,
    chain_id: row.id,
    is_deposit: row.is_deposit == 1 ? true : false,
    is_withdraw: row.is_withdraw == 1 ? true : false,
  });
  dialogFormVisible.value = true;
};

// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;

  try {
    const res = await mainnetListApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
  } catch (error) {
    loading.value = false;
  }
};

// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};

// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
});
</script>
